<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="../../template/tagedu.xhtml">
    <ui:define name="title">
        <title><h:outputText value="TAG - Edição Humano"/></title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta>
        <f:metadata>
            <f:viewParam name="human" value="#{humanEdit.humanID}"/>
        </f:metadata>
    </ui:define>

    <ui:define name="content">
        <f:event type="preRenderComponent" listener="#{humanEdit.initialize()}"/>
        <h:outputText id="title" styleClass="personName" value="Edição de #{humanEdit.human.name}"/>
        <h:form id="humanEditForm">
            <p:messages id="msg"/>

            <p:tabView id="humanTabView">
                <p:tab title="Dados Principais">
                    <p:panel styleClass="mainInfoTab">                        
                        <h:panelGrid  columns="2">
                            <h:outputLabel for="name" value="Nome:"/>
                            <p:inputText id="name" value="#{humanEdit.human.firstName}" styleClass="name" 
                                         required="true" requiredMessage="'Nome' é obrigatório"/>

                            <h:outputLabel for="lastName" value="Sobrenome:"/>
                            <p:inputText id="lastName" value="#{humanEdit.human.lastName}" styleClass="name" 
                                         required="true" requiredMessage="'Sobrenome' é obrigatório"/>

                            <h:outputLabel for="sex" value="Sexo:"/>
                            <h:selectOneMenu id="sex" value="#{humanEdit.human.sex}">
                                <f:selectItems value="#{humanEdit.sexList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="birthday" value="Nascimento:"/>
                            <p:inputMask id="birthday" mask="99/99/9999" value="#{humanEdit.human.birthday.time}"
                                         required="true" requiredMessage="'Nascimento' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                            <h:outputLabel for="nacionality" value="Nacionalidade:"/>
                            <h:selectOneMenu id="nacionality" value="#{humanEdit.nationalityID}">
                                <f:selectItems value="#{humanEdit.nationalityList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="ethnicity" value="Etnia:"/>
                            <h:selectOneMenu id="ethnicity" value="#{humanEdit.ethnicityID}">
                                <f:selectItems value="#{humanEdit.ethnicityList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="phone" value="Telefone Fixo:"/>
                            <p:inputMask id="phone" mask="(99)9999-9999" value="#{humanEdit.human.phone}"/>

                            <h:outputLabel for="celphone" value="Celular:"/>
                            <p:inputMask id="celphone" mask="(99)9999-9999" value="#{humanEdit.human.celphone}"/>

                            <h:outputLabel for="email" value="Email:"/>
                            <p:inputText id="email" value="#{humanEdit.human.email}"/>

                            <h:outputLabel for="laterality" value="Lateralidade:"/>
                            <h:selectOneMenu id="laterality" value="#{humanEdit.human.laterality}">
                                <f:selectItems value="#{humanEdit.lateralityList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="maritalStatus" value="Estado Civil:"/>
                            <h:selectOneMenu id="maritalStatus" value="#{humanEdit.human.maritalStatus}">
                                <f:selectItems value="#{humanEdit.maritalStatusList}"/>
                            </h:selectOneMenu>

                        </h:panelGrid>
                    </p:panel>
                </p:tab>
                <p:tab title="Endereço">
                    <p:panel id="address" styleClass="addressPanel">
                        <p:message for="cep"/>
                        <h:outputLabel value="CEP" for="cep"/>
                        <p:inputMask id="cep" value="#{humanEdit.cep}" mask="99999999" required="true" requiredMessage="'CEP' é obrigatório"/>
                        <p:commandButton value="Buscar" action="#{humanEdit.searchCEP()}" update="address" process="@this, cep"/>
                        <br/>
                        <h:panelGroup id="zipcode">
                            <h:outputLabel value="Estado:" for="state"/>
                            <p:inputText id="state" value="#{humanEdit.zipcode.city.state.name}" required="true" readonly="true"/>
                            <br/>
                            <h:outputLabel value="Cidade:" for="city"/>
                            <p:inputText id="city" value="#{humanEdit.zipcode.city.name}" required="true" readonly="true"/>
                            <br/><br/>
                        </h:panelGroup>
                        <h:panelGroup rendered="#{humanEdit.showAddress}">
                            <h:outputLabel value="Bairro:" for="neighborhood"/>
                            <p:autoComplete id="neighborhood" value="#{humanEdit.neighborhood}" 
                                            completeMethod="#{humanEdit.neighborhoodComplete}" 
                                            required="true" requiredMessage="'Bairro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/>
                            <h:outputLabel value="Logradouro:" for="street"/>
                            <p:autoComplete id="street" value="#{humanEdit.street}" 
                                            completeMethod="#{humanEdit.streetComplete}" 
                                            required="true" requiredMessage="'Logradouro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/><br/>
                            <h:outputLabel value="Número:" for="number"/>
                            <p:inputText id="number" value="#{humanEdit.address.number}"/>
                            <br/>
                            <h:outputLabel value="Complemento" for="complement"/>
                            <p:inputText id="complement" value="#{humanEdit.address.complement}"/>                            
                        </h:panelGroup>
                    </p:panel>
                </p:tab>
                <p:tab rendered="#{not empty humanEdit.additionalInfoList}" title="Dados Específicos">
                    <p:panel>                        
                        <ui:repeat value="#{humanEdit.additionalInfoList}" var="additionalInfo">
                            <h:panelGrid columns="2">
                                <h:outputText value="#{additionalInfo.additionalInfoType.acronym}:"/>
                                <p:inputMask mask="#{additionalInfo.additionalInfoType.mask}" value="#{additionalInfo.value}" 
                                             required="#{additionalInfo.additionalInfoType.compulsory}"
                                             requiredMessage="'#{additionalInfo.additionalInfoType.acronym}' é obrigatório"/>
                            </h:panelGrid>
                        </ui:repeat>
                    </p:panel>
                </p:tab>
                <p:tab title="Filiação">
                    <h:panelGroup id="filiation">
                        <p:panel header="Pai" styleClass="humanFiliation">
                            <p:commandLink value="Buscar"
                                           process="@this"
                                           oncomplete="searchHuman.show();" 
                                           action="#{humanEdit.prepareFatherSearch()}"
                                           update=":searchHumanDialog"/>
                            
                            <p:commandLink value="Cadastrar"
                                           process="@this"
                                           oncomplete="addHuman.show();" 
                                           action="#{humanEdit.prepareNewMale()}"
                                           update=":addHumanDialog"/>
                            <div style="clear:both"></div>
                            <br/>
                            <h:outputLabel value="Nome: "/>
                            <h:outputText rendered="#{humanEdit.human.father != null}" 
                                          value="#{humanEdit.human.father.name}"/>
                        </p:panel>
                        <p:panel header="Mãe" styleClass="humanFiliation">
                            <p:commandLink value="Buscar"
                                           process="@this"
                                           oncomplete="searchHuman.show();" 
                                           action="#{humanEdit.prepareMotherSearch()}"
                                           update=":searchHumanDialog"/>
                            
                            <p:commandLink value="Cadastrar"
                                           process="@this"
                                           oncomplete="addHuman.show();" 
                                           action="#{humanEdit.prepareNewFemale()}"
                                           update=":addHumanDialog"/>
                            <div style="clear:both"></div>
                            <br/>
                            <h:outputLabel value="Nome: "/>
                            <h:outputText rendered="#{humanEdit.human.mother != null}"
                                          value="#{humanEdit.human.mother.name}"/>
                        </p:panel>
                        <p:panel header="Responsável" styleClass="humanFiliation">
                            <p:commandLink value="Buscar"
                                           process="@this"
                                           oncomplete="searchHuman.show();" 
                                           action="#{humanEdit.prepareSponsorSearch()}"
                                           update=":searchHumanDialog"/>
                            
                            <p:commandLink value="Cadastrar"
                                           process="@this"
                                           oncomplete="addHuman.show();" 
                                           action="#{humanEdit.prepareNewHuman()}"
                                           update=":addHumanDialog"/>
                            <div style="clear:both"></div>
                            <br/>
                            <h:outputLabel value="Nome: "/>
                            <h:outputText rendered="#{humanEdit.human.sponsor != null}"
                                          value="#{humanEdit.human.sponsor.name}"/>
                        </p:panel>
                    </h:panelGroup>
                    <div style="clear:both;"></div>
                </p:tab>
            </p:tabView>
            <div class="buttonGroup">
                <p:commandButton value="Editar" update=":title msg" action="#{humanEdit.edit()}"/>
            </div>
        </h:form>
    </ui:define>

    <ui:define name="modals">
        <p:dialog id="searchHumanDialog" position="center" modal="true" 
                  widgetVar="searchHuman" closable="true" 
                  resizable="false" width="800" styleClass="noTitle">
            <h:form>
                <p:dataTable value="#{humanEdit.humanList}" var="human" paginator="true" rows="10" lazy="true"
                             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                             rowsPerPageTemplate="10,25,50"
                             selection="#{humanEdit.genericHuman}" rowKey="#{human.id}">
                    <p:column selectionMode="single" />
                    <p:column rendered="false">
                        <h:outputText value="#{human.id}"/>
                    </p:column>
                    <p:column>
                        <p:rowToggler/>
                    </p:column>

                    <p:column filterMatchMode="contains" filterStyle="width: 200px" 
                              filterBy="#{human.name}">
                        <f:facet name="header">
                            <h:outputText value="Nome"/>
                        </f:facet>
                        <h:outputText value="#{human.name}"/>
                    </p:column>
                    <p:rowExpansion>
                        <h:panelGrid columns="2" cellpadding="4">
                            <h:outputLabel value="Nascimento: " for="birthday"/>
                            <h:outputText value="#{human.birthday.time}" id="birthday">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </h:outputText>
                            <h:outputLabel value="Endereço: " for="address"/>
                            <h:outputText value="#{human.address.street.name}, #{humanaddress.number}.
                                          #{human.address.complement}. #{human.address.zipcode.city.name}/#{human.address.zipcode.city.state.name}" id="address"/>
                            <ui:repeat value="#{humanEdit.personAdditionalInfo(human.id)}" var="addInfo">
                                <h:outputLabel value="#{addInfo.additionalInfoType.acronym}: "/>
                                <h:outputText value="#{addInfo.value}"/>
                            </ui:repeat>
                        </h:panelGrid>
                    </p:rowExpansion>
                </p:dataTable>
                <div class="buttonGroupModal">
                    <p:commandButton value="OK" oncomplete="searchHuman.hide();" process="@form" 
                                     action="#{humanEdit.configureFatherMotherSponsor()}" 
                                     update=":humanEditForm:humanTabView:filiation"/>
                    <p:commandButton value="Cancelar" onclick="searchHuman.hide();"/>
                </div>
            </h:form>
        </p:dialog>


        <p:dialog id="addHumanDialog" position="center" modal="true" 
                  widgetVar="addHuman" closable="true" resizable="true" 
                  width="800" styleClass="noTitle">
            <h:form id="addHumanForm">
                <p:messages/>
                <p:tabView id="addHumanView">
                    <p:tab title="Dados Principais">
                        <p:panel styleClass="mainInfoTab">
                            <h:panelGrid columns="2">
                                <h:outputLabel for="humanName" value="Nome*:"/>
                                <p:inputText id="humanName" value="#{humanEdit.humanRegister.firstName}"
                                             required="true" requiredMessage="'Nome' é obrigatório"/>
                                <h:outputLabel for="humanLastName" value="Sobrenome*:"/>
                                <p:inputText id="humanLastName" value="#{humanEdit.humanRegister.lastName}"
                                             required="true" requiredMessage="'Sobrenome' é obrigatório"/>
                                <h:outputLabel for="humanSex" value="Sexo:"/>
                                <h:selectOneMenu id="humanSex" value="#{humanEdit.humanRegister.sex}">
                                    <f:selectItems value="#{humanEdit.sexListRegister}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanBirthday" value="Nascimento*:"/>
                                <p:inputMask id="humanBirthday" mask="99/99/9999" value="#{humanEdit.humanRegister.birthday.time}"
                                             required="true" requiredMessage="'Nascimento' é obrigatório">
                                    <f:convertDateTime pattern="dd/MM/yyyy"/>
                                </p:inputMask>
                                <h:outputLabel for="humanNacionality" value="Nacionalidade:"/>
                                <h:selectOneMenu id="humanNacionality" value="#{humanEdit.nationalityIDRegister}">
                                    <f:selectItems value="#{humanEdit.nationalityList}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanEthnicity" value="Etnia:"/>
                                <h:selectOneMenu id="humanEthnicity" value="#{humanEdit.ethnicityIDRegister}">
                                    <f:selectItems value="#{humanEdit.ethnicityList}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanPhone" value="Telefone Fixo:"/>
                                <p:inputMask id="humanPhone" mask="(99)9999-9999" value="#{humanEdit.humanRegister.phone}"/>
                                <h:outputLabel for="humanCelphone" value="Celular:"/>
                                <p:inputMask id="humanCelphone" mask="(99)9999-9999" value="#{humanEdit.humanRegister.celphone}"/>
                                <h:outputLabel for="humanEmail" value="Email:"/>
                                <p:inputText id="humanEmail" value="#{humanEdit.humanRegister.email}"/>
                                <h:outputLabel for="humanLaterality" value="Lateralidade:"/>
                                <h:selectOneMenu id="humanLaterality" value="#{humanEdit.humanRegister.laterality}">
                                    <f:selectItems value="#{humanEdit.lateralityList}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanMaritalStatus" value="Estado Civil:"/>
                                <h:selectOneMenu id="humanMaritalStatus" value="#{humanEdit.humanRegister.maritalStatus}">
                                    <f:selectItems value="#{humanEdit.maritalStatusList}"/>
                                </h:selectOneMenu>                
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>
                    <p:tab title="Endereço">
                        <p:panel id="address" styleClass="addressPanel">
                            <p:message for="cep"/>
                            <h:outputLabel value="CEP*" for="cep"/>
                            <p:inputMask id="cep" value="#{humanEdit.cepRegister}" mask="99999999" required="true" requiredMessage="'CEP' é obrigatório"/>
                            <h:commandButton value="Buscar">
                                <f:ajax execute="cep" render="address" listener="#{humanEdit.searchCEPRegister()}"/>
                            </h:commandButton>
                            <br/>
                            <h:panelGroup id="zipcode">
                                <h:outputLabel value="Estado*:" for="state"/>
                                <p:inputText id="state" value="#{humanEdit.zipcodeRegister.city.state.name}" required="true" readonly="true"/>
                                <br/>
                                <h:outputLabel value="Cidade*:" for="city"/>
                                <p:inputText id="city" value="#{humanEdit.zipcodeRegister.city.name}" required="true" readonly="true"/>
                                <br/><br/>
                            </h:panelGroup>
                            <h:panelGroup rendered="#{humanEdit.showAddressRegister}">
                                <h:outputLabel value="Bairro*:" for="neighborhood"/>
                                <p:autoComplete id="neighborhood" value="#{humanEdit.neighborhoodRegister}" 
                                                completeMethod="#{humanEdit.neighborhoodCompleteRegister}" 
                                                required="true" requiredMessage="'Bairro' é campo obrigatório"
                                                minQueryLength="3"/>
                                <br/>
                                <h:outputLabel value="Logradouro*:" for="street"/>
                                <p:autoComplete id="street" value="#{humanEdit.streetRegister}" 
                                                completeMethod="#{humanEdit.streetCompleteRegister}" 
                                                required="true" requiredMessage="'Logradouro' é campo obrigatório"
                                                minQueryLength="3"/>
                                <br/><br/>
                                <h:outputLabel value="Número:" for="number"/>
                                <p:inputText id="number" value="#{humanEdit.addressRegister.number}"/>
                                <br/>
                                <h:outputLabel value="Complemento" for="complement"/>
                                <p:inputText id="complement" value="#{humanEdit.addressRegister.complement}"/>                            
                            </h:panelGroup>
                        </p:panel>
                    </p:tab>    
                    <p:tab title="Dados Específicos">
                        <p:panel>
                            <h:panelGrid columns="2" id="addInfoPanel">
                                <h:selectOneMenu id="addInfoTypes" value="#{humanEdit.addInfoTypeIDRegister}">
                                    <f:selectItems value="#{humanEdit.additionalInfoTypeListRegister}"/>
                                    <f:ajax execute="addInfoTypes" render="addInfoPanel" listener="#{humanEdit.loadMask(humanEdit.addInfoTypeIDRegister)}"/>
                                </h:selectOneMenu>
                                <p:inputMask mask="#{humanEdit.mask}" value="#{humanEdit.additionalInfoRegister}"/>
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>                 
                </p:tabView>
                <div class="buttonGroupModal">
                    <p:commandButton value="OK" oncomplete="handleSubmitRequest(xhr, status, args)"
                                     ajax="true" process="@form" 
                                     action="#{humanEdit.createNewHuman()}" 
                                     update=":humanEditForm:humanTabView:filiation @form"/>
                    <p:commandButton value="Cancelar" onclick="addHuman.hide();"/>
                </div>
            </h:form> 
        </p:dialog>

        <script type="text/javascript">  
            function handleSubmitRequest(xhr, status, args) {  
                if(!args.validationFailed) {                  
                    addHuman.hide();
                }  
            }
        </script>
    </ui:define>
</ui:composition>